//==================================================================
//	Copyright (C) 2006  Davide Pasca
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//==================================================================
///
///
///
///
//==================================================================

//==================================================================
struct VSIN
{
    float3	_pos		: POSITION;
};

//==================================================================
struct VSOUT
{
    float4	_pos		: POSITION;
    float2	_tex		: TEXCOORD0;
};

//==================================================================
/// Global constants
//==================================================================
static const int    MAX_SAMPLES            = 16;    // Maximum texture grabs
static const float  BRIGHT_PASS_THRESHOLD  = 1.0f;  // Threshold for BrightPass filter
static const float  BRIGHT_PASS_OFFSET     = 10.0f; // Offset for BrightPass filter

//==================================================================
uniform sampler2D	_color_texmap	: register(s0);
uniform sampler2D	_bloom_texmap	: register(s1);
uniform float4		_bloom_scale	: register(c0);
uniform float4		_sample_offsets[MAX_SAMPLES] : register(c1);	// 1..16
uniform float4		_sample_weights[MAX_SAMPLES] : register(c17);	// 17..33
uniform	float3		_bright_threshold : register(c34);

//==================================================================
///
//==================================================================
float4 HDRFunc_DS4x4Clamp_PS( const VSOUT input ) : COLOR
{
    const int	NUM_SAMPLES = 16;
    float4		out_col = 0.0;

	for (int i=0; i < NUM_SAMPLES; ++i)
	{
		out_col += tex2D( _color_texmap, input._tex + _sample_offsets[i].xy );
	}
    out_col *= 1.0 / NUM_SAMPLES;

	out_col.xyz -= _bright_threshold;//float3( BRIGHT_PASS_THRESHOLD ,BRIGHT_PASS_THRESHOLD, BRIGHT_PASS_THRESHOLD );
	out_col.xyz = max( out_col.xyz, float3(0,0,0) );
/*
	if ( out_col.x < BRIGHT_PASS_THRESHOLD &&
		 out_col.y < BRIGHT_PASS_THRESHOLD &&
		 out_col.z < BRIGHT_PASS_THRESHOLD )
		out_col = float4( 0, 0, 0, 0 );
*/
	return out_col;
}

//==================================================================
///
//==================================================================
float4 HDRFunc_PlainTexture_PS( const VSOUT input ) : COLOR
{
	return tex2D( _color_texmap, input._tex );
}

//==================================================================
///
//==================================================================
float4 HDRFunc_Bloom_PS( const VSOUT input ) : COLOR
{
	float4 out_col = 0.0f;

	// Perform a one-directional gaussian blur
	for (int i = 0; i < 15; ++i)
	{
		out_col += _sample_weights[i] * tex2D( _color_texmap, input._tex + _sample_offsets[i].xy );
	}

	return out_col;
}

/*
//==================================================================
///
//==================================================================
float4 HDRFunc_DS2x2_PS( const VSOUT input ) : COLOR
{
    const int	NUM_SAMPLES = 4;
    float4		out_col = 0.0;

	for (int i=0; i < NUM_SAMPLES; ++i)
	{
		out_col += tex2D( _color_texmap, input._tex + _sample_offsets[i].xy );
	}
    out_col *= 1.0 / NUM_SAMPLES;

	return out_col;
}
*/
//==================================================================
///
//==================================================================
VSOUT HDRFunc_T1_C1_VS(	const VSIN input )
{
	VSOUT  output;

	output._pos = float4( input._pos, 1.0f );
	output._tex = (input._pos.xy + float2( 1, -1 )) * float2( 0.5, -0.5 );

    return output;
}

//==================================================================
///
//==================================================================
float4 HDRFunc_FinalPass_PS( const VSOUT input ) : COLOR
{
    float4		out_col;

	out_col = tex2D( _color_texmap, input._tex ) +
			  tex2D( _bloom_texmap, input._tex ) * _bloom_scale;
			  
	return out_col;
}
